﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GDD2_Game3
{
    class Wall:GameObject
    {
		public bool CanMoveThrough { get; set; }
		public bool NonBounce { get; set; }
		public bool FullBounce { get; set; }

        public Wall(float x, float y, float width, float height)
            :base(x, y, width, height)
        {
            
        }
			/*FloatBox intersectBoxUnion = FloatBox.Empty;
            foreach (Wall wall in crashedInto)
            {
				intersectBoxUnion = FloatBox.Union(intersectBoxUnion, FloatBox.AbsoluteIntersection(wall.Box, Box));
			}
			if(!intersectBoxUnion.IsEmpty())
			{
				if(intersectBoxUnion.TopSideY % 50.0f == 0.0f && intersectBoxUnion.Height <= intersectBoxUnion.Width)// == wall.Box.TopSideY)
				{
					box = box.ShiftedBy(-Vector2.UnitY * intersectBoxUnion.Height);
					velocity *= new Vector2(1.0f, -0.25f);
				}
				if(intersectBoxUnion.BottomSideY % 50.0f == 0.0f && intersectBoxUnion.Height <= intersectBoxUnion.Width)// == wall.Box.BottomSideY)
				{
					box = box.ShiftedBy(Vector2.UnitY * intersectBoxUnion.Height);
					velocity *= new Vector2(1.0f, -0.25f);
				}
				if(intersectBoxUnion.LeftSideX % 50.0f == 0.0f && intersectBoxUnion.Width <= intersectBoxUnion.Height)// == wall.Box.LeftSideX)
				{
					box = box.ShiftedBy(-Vector2.UnitX * intersectBoxUnion.Width);
					velocity *= new Vector2(-0.25f, 1.0f);
				}
				if(intersectBoxUnion.RightSideX % 50.0f == 0.0f && intersectBoxUnion.Width <= intersectBoxUnion.Height)// == wall.Box.RightSideX)
				{
					box = box.ShiftedBy(Vector2.UnitX * intersectBoxUnion.Width);
					velocity *= new Vector2(-0.25f, 1.0f);
				}
			}*/


		public void update(Vehicle vehicle)
		{
			if(CanMoveThrough)
			{
				if(colliding(vehicle))
				{
					vehicle.IsSlowed = true;
				}
			}
			else
			{
				FloatBox intersectBox = FloatBox.AbsoluteIntersection(Box, vehicle.Box);
				if(!intersectBox.IsEmpty())
				{
					if(intersectBox.TopSideY % 50.0f == 0.0f && intersectBox.Height <= intersectBox.Width)// == wall.Box.TopSideY)
					{
						vehicle.collidedIntoWall(-Vector2.UnitY * intersectBox.Height, new Vector2(1.0f, NonBounce ? 0.0f : (FullBounce ? -1.0f : -0.25f)));
					}
					if(intersectBox.BottomSideY % 50.0f == 0.0f && intersectBox.Height <= intersectBox.Width)// == wall.Box.BottomSideY)
					{
						vehicle.collidedIntoWall(Vector2.UnitY * intersectBox.Height, new Vector2(1.0f, NonBounce ? 0.0f : (FullBounce ? -1.0f : -0.25f)));
					}
					if(intersectBox.LeftSideX % 50.0f == 0.0f && intersectBox.Width <= intersectBox.Height)// == wall.Box.LeftSideX)
					{
						vehicle.collidedIntoWall(-Vector2.UnitX * intersectBox.Width, new Vector2(NonBounce ? 0.0f : (FullBounce ? -1.0f : -0.25f), 1.0f));
					}
					if(intersectBox.RightSideX % 50.0f == 0.0f && intersectBox.Width <= intersectBox.Height)// == wall.Box.RightSideX)
					{
						vehicle.collidedIntoWall(Vector2.UnitX * intersectBox.Width, new Vector2(NonBounce ? 0.0f : (FullBounce ? -1.0f : -0.25f), 1.0f));
					}
				}
			}
		}

		public int numAdjacent(IEnumerable<Wall> walls)
		{
			int num = 0;
			foreach(Wall wall in walls)
			{
				if((wall.Box.Center - Box.Center).LengthSquared() < 3600.0f)
				{
					++num;
				}
			}
			return num;
		}
    }
}
